package org.NooLab.itexx.storage.nodes;

import java.util.ArrayList;

import org.NooLab.utilities.datatypes.IndexDistance;
import org.NooLab.utilities.datatypes.IndexedDistances;
import org.NooLab.utilities.datatypes.ValuePair;
import org.NooLab.utilities.datatypes.ValuePairs;




/**
 * 
 * 
 *
 */
public class HistogramCollector {

	ArrayList<String> headerRow = new ArrayList<String>();
	ArrayList<ArrayList<?>> doctable = new ArrayList<ArrayList<?>> (); 
	
	SomNodesDataConverter parent;
	
	// ========================================================================
	public HistogramCollector(SomNodesDataConverter parent) {
		// 
		this.parent = parent ;
	}
	// ========================================================================

	@SuppressWarnings("unchecked")
	public void add(long somid, long docid, DocNodesDescription docNodesDescr) {
		
		ArrayList dtRow = new ArrayList();
		ValuePair vp;
		String str;
		Long cc,markovid=0L;
		
		// ArrayList<ValuePair> histobins;
		ValuePairs histobins;
		IndexedDistances docsomstats ;
		
		histobins = docNodesDescr.histogram;
		docsomstats = docNodesDescr.docsomStats ;
		
		String[] statsHeaders = new String[]{"nodesizetotal", "docnvariety","docnvarivar","docpvariety","docpvarivar","docabundance","histogramsize" };
		
		if (doctable.size()==0){   // this should be dynamic, by asking the db or the xml definition
			headerRow.add("id");
			headerRow.add("somid");
			headerRow.add("docid");
			
			headerRow.add("markovid");
			
			headerRow.add( statsHeaders[0] );
			headerRow.add( statsHeaders[1] );                 
			headerRow.add( statsHeaders[2] );
			headerRow.add( statsHeaders[3] );
			headerRow.add( statsHeaders[4] );
			headerRow.add( statsHeaders[5] );   
			headerRow.add( statsHeaders[6] );
			
			headerRow.add("createtime");
			headerRow.add("status");
			headerRow.add("vanishtime");
			
			
			
			// get the header from the "histobins"
			for (int i=0;i<histobins.size();i++){
				vp = histobins.getItem(i);
				str = "n_"+ vp.getValue1();
				headerRow.add(str);
			}// i->
			doctable.add(headerRow) ;
		}
		
		dtRow.add((double) doctable.size());
		dtRow.add( (double) somid );
		dtRow.add( (double) docid );
		dtRow.add( (double) markovid );

		// adding the numerical description
		for (int i=0;i< statsHeaders.length; i++){
			int p = docsomstats.getIndexByStr(  statsHeaders[i]);// "nodesizetotal" ...
			double v = docsomstats.getItem(p).getDistance() ;
			dtRow.add( v ) ;
		}
		
		// adding status info & time stamp
		
		
		dtRow.add( (double)(1.0)*System.currentTimeMillis()); // createtime
		dtRow.add( 1.0) ; // status
		dtRow.add( 0.0) ; // vanishtime
		
		
		// adding the histogram
		for (int i=0;i<histobins.size();i++){
			
			vp = histobins.getItem(i);
			cc = (Long) vp.getValue2();
			dtRow.add((double)(1.0 * cc));
			
		}// i->
		
		doctable.add(dtRow) ;
		// note that the index value that we use here is only preliminary... 
		// on insert update into the final table the index will change
	}
	
	
}













